package com.quxian.chambercommerce.admin_action;


import java.io.ByteArrayInputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpSession;

import com.opensymphony.xwork2.ActionContext;
import com.quxian.chambercommerce.base.BaseAction;
import com.quxian.chambercommerce.bean.ModleEntity;
import com.quxian.chambercommerce.bean.StaffInfoEntity;
import com.quxian.chambercommerce.service.LoginService;
import com.quxian.chambercommerce.util.RandomNumUtil;
import com.quxian.chambercommerce.util.StringUtil;

public class AdminLoginAction extends BaseAction{

	private static final long serialVersionUID = 1253164626342724654L;
	DateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); 
	private String nowTime;
	private LoginService loginService;
	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}
	
	private ModleEntity modleEntity;
	private StaffInfoEntity staffInfoEntity;
	private String userName;
	private String password;
	private String verificationCode;
	private String errorMessage;
	
	public String getNowTime() {
		return nowTime;
	}
	public void setNowTime(String nowTime) {
		this.nowTime = nowTime;
	}
	public ModleEntity getModleEntity() {
		return modleEntity;
	}
	public void setModleEntity(ModleEntity modleEntity) {
		this.modleEntity = modleEntity;
	}
	public StaffInfoEntity getStaffInfoEntity() {
		return staffInfoEntity;
	}
	public void setStaffInfoEntity(StaffInfoEntity staffInfoEntity) {
		this.staffInfoEntity = staffInfoEntity;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getErrorMessage() {
		return errorMessage;
	}
	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}
	public LoginService getLoginService() {
		return loginService;
	}
	public String getVerificationCode() {
		return verificationCode;
	}
	public void setVerificationCode(String verificationCode) {
		this.verificationCode = verificationCode;
	}
	
	public String login() {
		try{
			HttpSession session = request.getSession();
			if(StringUtil.isEmpty(userName)) {
				errorMessage = "用户不能为空";
				return ERROR;
			}
			
			if(StringUtil.isEmpty(password)) {
				errorMessage = "密码不能为空";
				return ERROR;
			}
			
			if(StringUtil.isEmpty(verificationCode)){
				errorMessage = "验证码不能为空";
				return ERROR;
			}
			if(!session.getAttribute("random").equals(verificationCode)){
				errorMessage = "验证码输入错误";
				return ERROR;
			}
			
		    
//			password = ThreeDES.getEncString(password);
			staffInfoEntity = loginService.login(userName, password);
			Date mdate = new Date();
			nowTime = format.format(mdate);
			
			
			session.setAttribute("user",staffInfoEntity);
			
		} catch (Exception e) {
			
			errorMessage = e.getMessage();
			return ERROR;
		}
		return SUCCESS;
	}
	
	
	
	private ByteArrayInputStream inputStream;    
	
	public String verification() throws Exception{  
		RandomNumUtil rdnu=RandomNumUtil.Instance();    
		System.out.println("=====rdnu====="+rdnu.getString());
		this.setInputStream(rdnu.getImage());
		
		ActionContext.getContext().getSession().put("random", rdnu.getString());
		return SUCCESS;    
	}    
	
	public void setInputStream(ByteArrayInputStream inputStream) {    
		this.inputStream = inputStream;    
	}    
	
	public ByteArrayInputStream getInputStream() {    
		return inputStream;    
	}   
	
	
}
